<template>
	<view class="content">
		<view class="info-block">
			<view class="title">意见反馈</view>
			<view class="form-item">
				<view class="form-label">反馈分类</view>
				<view class="form-content">
					<picker class="form-ctrl" :range="list" range-key="name" :value="Index" @change="bindType">
						{{list[typeIndex]['name']}}
					</picker>
					<view class="iconfont icon-gengduo1 arrow"></view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">反馈内容</view>
				<view class="form-content">
					<textarea v-model="form.content" class="form-ctrl" maxlength="-1" placeholder="请输入您的意见"
						auto-focus='true'></textarea>
				</view>
			</view>

			<view class="title1">图片上传</view>
			<view class="upload-item">
				<view v-for="(item,index) in imgArr" class="image-item">
					<image :src="item" @click="previewImg(item)" class="img">
					</image>
					<view @click="closeImg(index)" style="display: inline;">
						<view class="iconfont">
							<view class="delete icon-guanbi"></view>
						</view>
					</view>
				</view>
				<view class="upload-content" @click="chooseImg" v-if="imgArr.length<1">
					<image src="https://www.mashangjiuye.cn/file/91885/20230302173239.png" class="upload">
					</image>
				</view>
			</view>

			<view class="btn-item">
				<view class="btn-save" @click="openItem">保存</view>
			</view>
		</view>
		<view class="mcaptcha-content" v-if="displayChoose">
			<view class="mcaptcha-item">
				<view class="verify-item">
					<input v-model="form.mcaptcha" type="text" class="mcaptcha-crl" placeholder="请输入验证码" />
					<view class="code-img-wrapper" @click="updateImageCode">
						<canvas style="width:220rpx;height:86rpx;" canvas-id="canvas"></canvas>
					</view>
				</view>
				<view class="btn-item">
					<view class="btn-save" @click="submit">确定</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import config from '@/config.js'
	import {
		Mcaptcha
	} from './mcaptcha.js'
	import {
		validate
	} from '@/util/validate.js'
	import {
		opinionAddApi,
		getTypeListApi
	} from "@/api/user";
	export default {
		data() {
			return {
				form: {
					mcaptcha: '',
					content: '',
					imgUrls: '',
					type: null
				},
				isTest: 0,
				imgArr: [],
				imageUrl: '',
				displayChoose: false,
				typeIndex: 0,
				list: [],
				rules: {
					content: {
						name: '意见反馈',
						required: true
					}
				}
			}
		},
		onReady() {
			this.mcaptcha = new Mcaptcha({
				el: 'canvas',
				width: 120,
				height: 35,
				createCodeImg: ""
			});
		},
		methods: {
			bindType(e) {
				this.typeIndex = e.detail.value;
			},
			getList() {
				getTypeListApi({}).then((res) => {
					this.list = res ?? [];
				})
			},
			openItem() {
				if (validate(this.form, this.rules)) {
					if (this.isTest === 1) {
						for (let i = 0; i < this.imgArr.length; i++) {
							this.form.imgUrls += this.imgArr[i] + ',';
						}
						this.form.type = this.list[this.typeIndex].id
						opinionAddApi(this.form).then(() => {
							uni.$emit('refresh', 4)
							this.showToast('保存成功', () => {
								uni.navigateBack()
							})
						})

					} else {
						this.displayChoose = true
					}

				}
			},
			chooseImg() {
				uni.chooseImage({
					count: 6, // 最多可以选择的图片张数，默认9
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					uplodaType: 3,
					success: res => {
						uni.uploadFile({
							url: config.requestUrl + '/upload?uploadType=2', //post请求的地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {},
							header: {
								// Authorization: that.authtoken
							},
							success: (myres) => {
								const data = JSON.parse(myres.data);
								if (this.imgArr.length === 0) {
									this.imgArr = this.imgArr.concat(data.data)
								} else if (this.imgArr.length < 6) {
									this.imgArr = this.imgArr.concat(data.data) //concat追加到数组
								}
								console.log(this.imgArr)
							}
						})
					}
				})
			},
			closeImg(index) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除当前图片',
					success: function(res) {
						if (res.confirm) {
							that.imgArr.splice(index, 1);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 刷新验证码
			updateImageCode() {
				this.mcaptcha.refresh()
			},
			// 提交前校验图形验证码
			submit() {
				if (!this.form.mcaptcha) {
					uni.showToast({
						title: '请输入图形验证码',
						icon: 'none'
					})
				}
				let validate = this.mcaptcha.validate(this.form.mcaptcha)
				if (!validate) {
					uni.showToast({
						title: '图形验证码错误',
						icon: 'none'
					})
				} else {
					this.isTest = 1;
					this.displayChoose = false
				}
			}
		},
		onLoad() {
			this.imageUrl = this.globalConfig.imageUrl;
			this.getList()
		},
	}
</script>

<style lang="scss">
	.content {
		width: 100%;
		padding: 40rpx;
		background: #fff;

		.info-block {
			display: flex;
			flex-direction: column;

			.title {
				font-size: 40rpx;
				font-weight: bold;
			}

			.form-item {
				padding-top: 30rpx;

				.form-content {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					border-bottom: 1px solid #efefef;

					.form-ctrl {
						width: 100%;
						padding-bottom: 30rpx;
						margin-top: 30rpx;
						color: #999;
					}

					.iconfont {
						margin-top: 30rpx;
					}
				}

				.form-content:last-child {
					border-bottom: none;
				}
			}

			.title1 {
				font-size: 34rpx;
				margin-top: 20rpx;
				margin-bottom: 10rpx;
			}

			.upload-item {
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;


				.image-item {
					display: flex;
					flex-direction: row;
					margin-right: 30rpx;
					padding: 8rpx;

					.img {
						width: 286rpx;
						height: 200rpx;
					}

					.delete {
						margin-top: 10rpx;
						margin-left: -40rpx;
						width: 20px;
						height: 20px;
						position: absolute;
					}
				}

				.upload-content {
					width: 300rpx;
					height: 200rpx;
					background: #F9F9F9;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-top: 10rpx;

					.upload {
						width: 100rpx;
						height: 100rpx;
					}
				}
			}


			.btn-item {
				display: flex;
				justify-content: center;

				.btn-save {
					display: flex;
					align-items: center;
					justify-content: center;
					margin-top: 140rpx;
					height: 70rpx;
					width: 420rpx;
					border-radius: 40rpx;
					margin-bottom: 20rpx;
					background-color: #FE7526;
					color: #fff;
				}
			}
		}

		.mcaptcha-content {
			position: fixed;
			left: 0;
			top: 0;
			background: rgba(0, 0, 0, 0.2);
			z-index: 999;
			width: 100vw;
			height: 100vh;

			.mcaptcha-item {
				width: 100%;
				height: auto;
				background: #fff;
				z-index: 9999;
				position: fixed;
				transform: translate(-50%, 0%);
				left: 50%;
				bottom: 50%;
				display: flex;
				flex-direction: column;
				box-shadow: 0 0 10px 0px #dadada;
				display: flex;
				flex-direction: column;
				align-content: center;

				.verify-item {
					display: flex;
					flex-direction: row;
					margin-top: 40rpx;
					justify-content: center;

					.mcaptcha-crl {
						width: 300rpx;
						height: 60rpx;
						border: 1rpx solid #999;
						margin-right: 20rpx;
						padding-left: 3rpx;
					}
				}

				.btn-item {
					display: flex;
					justify-content: center;

					.btn-save {
						display: flex;
						align-items: center;
						justify-content: center;
						margin-top: 40rpx;
						height: 50rpx;
						width: 160rpx;
						border-radius: 10rpx;
						margin-bottom: 20rpx;
						border: 1rpx solid #FE7526;
						color: #FE7526;
					}
				}
			}
		}
	}
</style>